import Vue  from'vue'
import Vuex from 'vuex'
Vue.use(Vuex);


const state =sessionStorage.getItem('state')!=''?JSON.parse(sessionStorage.getItem('state')): {
  user:{
    userId:'',
    username:'',
    password:'',
    phone:'',
    qqEmail:'',
    isAdmin:null,
  },
  token:''
};
const getters= {
  getUser:(state)=>()=>{
    let user= state.user
     return user
  },
  getToken:(state)=>()=>{
    let token=state.token
     return token
  }
};
const mutations = {
  updateUser(state,user){
    state.user=user
  },
     // 修改token，并将token存入localStorage
  updateToken(state,token){
       state.token=token,
       sessionStorage.setItem('Authorization', token)
  },
  delToken(state) {
    state.token = '',
    state.user=null,
    sessionStorage.removeItem('Authorization')
  }
};
const actions={
  asyncUpdateUser(context,user){
    context.commit('updateUser',user)
  },

  asyncUpdateToken(context,token){
  context.commit('updateToken',token)
 },
  asyncDelToken(context){
  context.commit('delToken')
 }
};
export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions

})
